package com.gym.baselib.base

import android.content.Intent
import android.content.res.Resources
import android.graphics.Color
import android.os.Bundle
import android.view.*
import android.widget.FrameLayout
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.AdaptScreenUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.githang.statusbar.StatusBarCompat
import com.google.gson.reflect.TypeToken
//import com.google.gson.reflect.TypeToken
import com.xgst.land.R
import com.xgst.land.utils.ProgressUtils

open abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {

    private lateinit var launch: ActivityResultLauncher<Intent>
    private var mOnActivityResult:(Intent?)->Unit = {}
    private var _binding : VB? = null
    protected val binding: VB
        get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = viewBinding()
        launch = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == RESULT_OK) {
                mOnActivityResult(it.data)
            }
        }
        val resId = getLayoutId()
        if(resId != -1){
            setContentView(R.layout.view_stub_layout)
            val viewStub = findViewById<ViewStub>(R.id.viewStub)
            viewStub.postDelayed({
                _binding = viewBinding()
                viewStub.layoutResource = getLayoutId()
                viewStub.inflate()
            }, 300)
            viewStub.setOnInflateListener { stub, inflated ->
                findViewById<View>(R.id.tvViewStubProgress).visibility = View.GONE
                findViewById<View>(R.id.imgViewStubProgress).visibility = View.GONE
                step()
            }
        }else{
            _binding = viewBinding()
            setContentView(binding.root)
            step()
        }
    }

    abstract fun viewBinding(): VB

    private fun step(){
        StatusBarCompat.setStatusBarColor(this,Color.WHITE)
        val imgBack = findViewById<View>(R.id.imgBack)
        //返回
        imgBack?.setOnClickListener { finish() }
        init()
    }

    fun setTitle(title:String){
        val tvTitle = findViewById<TextView>(R.id.tvTitle)
        if(tvTitle != null){
            tvTitle.text = title
        }
    }

    fun setRight(value:String,vararg onRightClick:()->Unit){
        val tvRight = findViewById<TextView>(R.id.tvRight)
        if(tvRight != null){
            tvRight.text = value
            findViewById<FrameLayout>(R.id.flRight).setOnClickListener {
                if(onRightClick != null && onRightClick.size > 0){
                    onRightClick[0]()
                }
            }
            tvRight.setOnClickListener {
                if(onRightClick != null && onRightClick.size > 0){
                    onRightClick[0]()
                }
            }
        }
    }

    @LayoutRes
    open fun getLayoutId():Int  = -1

    abstract fun init()

    fun showProgress(isLoop:Boolean = false){
        ProgressUtils.showProgress(isLoop)
    }

    override fun onPause() {
        super.onPause()
        dismissProgress()
    }

    fun showToast(msg:String){
        ToastUtils.showShort(msg)
    }

    fun dismissProgress(){
        ProgressUtils.dismiss()
    }

    fun startActivity(clazz: Class<*>){
        startActivity(Intent(this,clazz))
    }

    fun startActivityForResult(clazz:Class<*>,onActivityResult:(Intent?)->Unit){
        startActivityForResult(Intent(this,clazz),onActivityResult)
    }

    fun startActivityForResult(intent:Intent,onActivityResult:(Intent?)->Unit){
        mOnActivityResult = onActivityResult
        launch.launch(intent)
    }

    override fun getResources(): Resources {
        return AdaptScreenUtils.adaptWidth(super.getResources(),1280)
    }
    inline fun <reified T> genericType() = object : TypeToken<T>(){}.type
}

